package com.zsummer.design.structure.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * 享元模式
 * @author zsummer
 *
 */
public class Client {

    public static void main(String[] args) {
        Chess red = ChessFactory.getChess("红");
        red.setPoint(new int[]{1,2});
        red.move();

        Chess white = ChessFactory.getChess("白");
        white.setPoint(new int[]{2,4});
        white.move();
    }
}

interface behavior {
    void move();
}

/**
 * 棋子
 */
class Chess implements behavior{
    private String color;
    private int[] point;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int[] getPoint() {
        return point;
    }

    public void setPoint(int[] point) {
        this.point = point;
    }

    @Override
    public void move() {
        System.out.println(color + "棋落子到" + "[" + point[0] + "," + point[1] + "]");
    }
}

/**
 * 工厂
 */
class ChessFactory {
    private static Map<String, Chess> chessMap = new HashMap<>();

    public static Chess getChess(String color) {
        Chess chess = chessMap.get(color);
        if (chess == null) {
            chess = new Chess();
            chess.setColor(color);
            chessMap.put(color, chess);
        }
        return chess;
    }
}
